package cn.gson.boot.controller;

import cn.gson.boot.model.pojos.Supplier;
import cn.gson.boot.model.service.SupplierService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Controller
@RequestMapping("/supplier")
public class SupplierController {
    @Autowired
    SupplierService supplierService;
    /**
     * 查询所有的供应商
     * @return
     */
    @RequestMapping("/supplier-list")
    @ResponseBody
    public Map<String,Object> selectAllSuppliers(Integer pageNum, Integer pageSize, String search){
        Page<Object> obj = PageHelper.startPage(pageNum,pageSize);
        supplierService.selectAllSuppliers(search);
        Map<String,Object> map = new HashMap<>();
        map.put("total",obj.getTotal());
        map.put("rows",obj.getResult());
        return map;
    }

    /**
     * 查询所有的供应商
     * @param search
     * @return
     */
    @RequestMapping("/supplierAll")
    @ResponseBody
    public List<Supplier> selectAllSuppliers(String search){
        return supplierService.selectAllSuppliers(search);
    }

    /**
     * 新增供应商
     * @param supplier
     * @return
     */
    @RequestMapping("/addSupplier")
    @ResponseBody
    public void addSupplier(@RequestBody Supplier supplier){
        supplier.setSupplierSc(0);
        supplierService.addSupplier(supplier);
    }

    /**
     * 修改供应商
     * @param supplierId
     * @return
     */
    @RequestMapping("/upSupplier")
    @ResponseBody
    public List<Supplier> upSupplier(Integer supplierId){
        return supplierService.upSupplier(supplierId);
    }

    /**
     * 删除供应商
     * @param supplierId
     * @return
     */
    @RequestMapping("/delSupplier")
    @ResponseBody
    public void delSupplier(Integer supplierId){
        supplierService.delSupplier(supplierId);
    }
}
